import * as types from '../mutation-types'
import _ from 'lodash'
import api from 'api'
import CryptoJS from 'crypto-js'
import formatDate from 'filters/format-date'

// initial state
const state = {
  devices: [],
  devicesStatus: []
}

const methods = {
  // 根据源串获取参数
  getParameter (data) {
    // 做一次URL编码（为了配合对方C#后台，这里做映射）
    let str = data.replace(/=/g, '%3d').replace(/,/g, '%2c').replace(/\+/g, '%2b').replace(/&/g, '%26').replace(/ /g, '+').replace(/\//g, '%2f').replace(/:/g, '%3a')
    // sha1加密
    let hash = CryptoJS.SHA1(str)
    // sha1加密之后再进行Base64，再进行一次URL编码
    let signature = hash.toString(CryptoJS.enc.Base64).replace(/=/g, '%3d').replace(/,/g, '%2c').replace(/\+/g, '%2b').replace(/&/g, '%26').replace(/ /g, '+').replace(/\//g, '%2f').replace(/:/g, '%3a')
    // 生成最后的参数
    let parameter = `${data}&signature=${signature}`
    return parameter
  },
  getStreeStatusParameter () {
    // 测试账号
    let username = 'POLY'
    let appSecret = '575f6b8940d0a554766f1c871e2ca3d4'
    // 加密前的源串
    let data = `appKey=${username}&appSecret=${appSecret}`
    let parameter = methods.getParameter(data)
    return parameter
    // api.stree.getStatus(parameter).then((res) => {
    //   console.log(res)
    //   // test = res.data.Data.Items[2]
    // }).catch((res) => {
    //   console.log(res)
    // })
  },
  getMonitorParameter () {
    // 测试账号
    let username = 'POLY'
    let appSecret = '575f6b8940d0a554766f1c871e2ca3d4'
    // 设备id
    let deviceId = '3f644d71-0d4d-11e7-b1f6-b5c7abcb4f39'
    // 开始时间和结束时间
    let endDate = formatDate(new Date(), 'yyyy-MM-dd hh:mm', true)
    let startDate = formatDate(new Date() - 60 * 1000 * 60 * 24 * 90, 'yyyy-MM-dd hh:mm', true)
    console.log(startDate, endDate)
    // 页面index
    let pageIndex = '1'
    // 加密前的源串升序排列的这个但是因为参数固定(排序固定),偷懒没写
    let data = `appKey=${username}&deviceId=${deviceId}&endDate=${endDate}&pageIndex=${pageIndex}&startDate=${startDate}&appSecret=${appSecret}`
    let parameter = methods.getParameter(data)
    return parameter
  }
}

// actions
const actions = {
  updateStreeMonitor ({ commit }) {
    let parameter = methods.getMonitorParameter()
    api.stree.getMonitor(parameter).then((res) => {
      if (res.data) {
        let data = res.data
        // 返回最新(最后那条)的那条数据
        commit(types.UPDATE_STREE, data)
      }
    }).catch((res) => {
      console.log(res)
    })
  },
  updateStreeStatus ({ commit }, deviceStatus) {
    let parameter = methods.getStreeStatusParameter()
    api.stree.getStatus(parameter).then((res) => {
      if (res.data) {
        let data = res.data
        commit(types.UPDATE_STREE_STATUS, data)
      }
    }).catch((res) => {
      console.log(res)
    })
  }
}

// getters
const getters = {
  streeStatus () {
    if (!state.devicesStatus || state.devicesStatus.length === 0) {
      return
    }
    return state.devicesStatus[0].Status
  },
  streeMonitor () {
    if (!state.devices[0]) {
      return
    }
    return state.devices[0].Data.Items[state.devices[0].Data.Items.length - 1]
  },
  streeMonitorStatus () {
    if (!state.devices[0]) {
      return
    }
    return state.devices[0].status
  }
}

// mutations
const mutations = {
  [types.UPDATE_STREE] (state, obj) {
    let finded = _.find(state.devices, (item) => {
      return item.id === obj.id
    })
    if (finded) {
      state.devices = _.filter(state.devices, (item) => {
        return item.id !== obj.id
      })
    }
    state.devices.push(obj)
  },
  [types.UPDATE_STREE_STATUS] (state, obj) {
    let finded = _.find(state.devices, (item) => {
      return item.id === obj.id
    })
    if (finded) {
      state.devices = _.filter(state.devices, (item) => {
        return item.id !== obj.id
      })
    }
    state.devicesStatus.push(obj)
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
